package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.Article;
import com.zzc.fwzpxt.entity.ArticleTag;
import com.zzc.fwzpxt.entity.Tag;
import com.zzc.fwzpxt.mapper.ArticleMapper;
import com.zzc.fwzpxt.service.IArticleService;
import com.zzc.fwzpxt.service.IArticleTagService;
import com.zzc.fwzpxt.service.ITagService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import com.zzc.common.utils.StringUtil;
import com.zzc.sys.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章 article 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-09-26
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private ITagService iTagService;
    @Autowired
    private IArticleTagService iArticleTagService;

    /**
     * 添加文章视图
     * @return
     */
    @GetMapping(value = "/addArticle")
    public String addArticleView(Model model) {
        EntityWrapper<Tag> wrapper = new EntityWrapper<>();
        List<Tag> tags = iTagService.selectList(wrapper);
        model.addAttribute("tags", tags);
        return blogPath + "article/add_article";
    }

    /**
     * 添加文章
     * @param article
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addArticle")
    public JsonResult addArticle(Article article, Long[] tag) {
        return iArticleService.addArticle(article, tag) ? renderSuccess("发布成功") : renderError("发布失败");
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateArticle")
    public JsonResult updateArticle(Article article, Long[] tag) {
        return iArticleService.updateArticle(article, tag) ? renderSuccess("修改成功") : renderError("修改失败");
    }

    /**
     * 新闻列表视图
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return blogPath + "article/list";
    }

    /**
     * 新闻列表分页
     * @param pageNumber
     * @param pageSize
     * @param searchText
     * @return
     */
    @ResponseBody
    @PostMapping("/getList")
    public Map<String, Object> getUserList(int pageNumber, int pageSize, String searchText) {
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Article> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        if (StringUtil.isNotEmpty(searchText)) {
            wrapper.like("article_title", searchText);
        }
        wrapper.eq("status", 1);
        wrapper.orderBy("gmt_create", false);
        Page<Article> articlePage = iArticleService.selectPage(page, wrapper);
        result.put("total", articlePage.getTotal());
        result.put("rows", articlePage.getRecords());
        return result;
    }

    /**
     * 获取一篇文章进行修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/getOneArticle/{id}")
    public String  getOneArticle(@PathVariable("id") Integer id, Model model) {
        // 查询当前文章
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Article article = iArticleService.selectOne(wrapper);
        model.addAttribute("article", article);

        // 查询所有标签
        EntityWrapper<Tag> tagEntityWrapper = new EntityWrapper<>();
        List<Tag> tags = iTagService.selectList(tagEntityWrapper);
        model.addAttribute("tags", tags);

        // 查询关联标签
        EntityWrapper<ArticleTag> articleTagEntityWrapper = new EntityWrapper<>();
        articleTagEntityWrapper.eq("article_id", id);
        List<ArticleTag> articleTags = iArticleTagService.selectList(articleTagEntityWrapper);
        model.addAttribute("articleTags", articleTags);

        return blogPath + "article/update_article";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<Article> articles) {
        if (articles == null) {
            return renderError("请选择数据");
        }
        articles.forEach(a -> {
            a.setStatus(0);
        });

        return iArticleService.updateBatchById(articles)? renderSuccess("删除成功") : renderError("删除失败");
    }

}
